(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:CategoryCtr
     * @description
     * # CategoryCtr
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('CategoryCtrl', CategoryCtr);

    CategoryCtr.$inject = ['$scope', 'dataService', '$timeout', 'helper', '$rootScope', '$location'];

    function CategoryCtr($scope, dataService, $timeout, helper, $rootScope, $location) {


        //添加类目数据
        $scope.addAction = function() {
            $scope.cat = {};
            if (!$rootScope.store_id) {
                return helper.myAlert("非法门店id", "#/category/list");
            }
        }

        //创建类目
        $scope.categoryAdd = function() {
            if (!$scope.catId) {
                $scope.cat.store_id = $rootScope.store_id
                dataService.categoryGet({ query: { level: 1 } },0).then(function(r) {
                    // console.log("-------1级类目", r)
                    if (r.itemcats.length && r.itemcats[0].cid) {
                        dataService.categoryAdd({ name: $scope.cat.catName, store_id: $scope.cat.store_id, parent_cid: r.itemcats[0].cid }).then(function(r) {
                            if (r.cid) {
                                helper.myAlert("success", "#/category/list");
                            }
                        });
                    } else {
                        helper.myAlert("未找到根目录，请联系管理员！");
                    }
                });
            } else {

                dataService.categoryAdd({ name: $scope.catName, parent_cid: $scope.catId }).then(function(r) {
                    if (r.cid) {
                        helper.myAlert("success", "#/category/list");
                    }
                });
            }
        }

        //获取类目数据
        $scope.listAction = function() {
            $scope.$watch('chosedCat', function(newValue, oldValue, scope) {
                if (_.isObject(newValue)) {
                    $scope.name = newValue.name;
                    $scope.sort = newValue.sort;
                }
            }, true);
            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id
            });
        }


        //修改类目数据
        $scope.update = function($event) {
            var name = $scope.name;
            var sort = $scope.sort;
            if (!$scope.chosedCat) {
                helper.myAlert("No data found");
                return false;
            }
            var id = $scope.chosedCat.id;
            if (!name && !id) {
                helper.myAlert("No data found");
                return false;
            }
            dataService.categoryUpdate({ cid: id, name: name,sort:sort }).then(function(r) {
                if (r.cid) {
                    helper.myTip("success", $event.currentTarget);
                    $timeout(function() {
                        $scope.chosedCat = {};
                        $scope.listAction();
                    });
                }
            })
        }

        //删除类目
        $scope.delete = function($event) {
            if (!$scope.chosedCat) {
                helper.myAlert("No data found");
                return false;
            }
            var id = $scope.chosedCat.id;
            if (!id) {
                helper.myAlert("No data found");
                return false;
            }
            dataService.categoryDelete({ cid: id }).then(function(r) {
                if (r.cid) {
                    helper.myTip("success", $event.currentTarget);
                    $timeout(function() {
                        $scope.chosedCat = {};
                    });
                }
            })
        }

        $scope.relationAction = function() {
            $scope.cid = $location.search().id;
            var data = { query: { cid: $scope.cid } };
            dataService.categoryGet(data,0).then(function(r) {
                if (r.itemcats) {
                    $scope.cat = r.itemcats[0];
                }
            })
        }

        $scope.addRelation = function() {
            async.map($scope.label, function(da, call) {
                var data = { label_id: da.label_id, cat_id: $scope.cid, store_id: $rootScope.store_id };
                dataService.catLabelRelationAdd(data).then(function(r) {
                    call(null);
                })
            }, function(e, r) {
                helper.myAlert("绑定成功！", "#/category/list");
            });
        }
    }
})();